package com.aruba.girlanime.delegate;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.daimajia.androidanimations.library.BaseViewAnimator;
import com.daimajia.androidanimations.library.YoYo;

import static android.animation.ObjectAnimator.ofFloat;

/**
 * Created by Administrator on 2018/4/26.
 */

public class RobotAnimeDelegate implements IAnimeDelegate {
    View iv_come;
    View iv_robot;
    View rl_robot;
    private int paddingLeft = 100;
    private boolean isRunning;

    @Override
    public void startAnime() {
        if (!isRunning) {
            isRunning = true;
            YoYo.with(new RobotAnimator())
                    .playOn(iv_robot);
        }
    }

    @Override
    public void init(View... args) {
        iv_come = args[0];
        iv_robot = args[1];
        rl_robot = args[2];
    }

    @Override
    public void stopAnime() {

    }

    @Override
    public void reset() {

    }

    private float top;

    /**
     * robot的anime
     */
    public class RobotAnimator extends BaseViewAnimator {

        @Override
        public void prepare(View target) {
            target.setTranslationX(-target.getWidth() / 1.5f);
            AnimatorSet bouncer = new AnimatorSet();

            ObjectAnimator rotation = ofFloat(target, "rotation", 25, 35, 15);
//            rotation.setDuration(4000);
//            rotation.setInterpolator(new LinearInterpolator());
            ObjectAnimator translationX = ObjectAnimator.ofFloat(target, "translationX", -target.getWidth() / 1.5f, -target.getWidth());
//            translationX.setInterpolator(new LinearInterpolator());

            ObjectAnimator rotation1 = ofFloat(target, "rotation", 15, 45, 55, 0);

            //转加出来动画
            bouncer.play(rotation);
            bouncer.play(rotation1).with(translationX).after(rotation);

            ObjectAnimator translationYTemp = ObjectAnimator.ofFloat(target, "translationY", 0, -target.getHeight() / 2, 0);

//            bouncer.play(translationX).after(rotation);

            ObjectAnimator bounceInLeft = ObjectAnimator.ofFloat(target, "translationX", -target.getWidth() + paddingLeft, 30 + paddingLeft, paddingLeft);
            bouncer.play(bounceInLeft).with(translationYTemp)
                    .after(rotation1);

            top = -target.getY() + iv_come.getHeight() * 2;
            ObjectAnimator rotation2 = ObjectAnimator.ofFloat(target, "rotation", 0, 10, -10, 6, -6, 3, -3, 0);
            ObjectAnimator translationY = ObjectAnimator.ofFloat(target, "translationY", 0,
                    top);
            translationY.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    iv_come.setTranslationY(0);
                    iv_come.setTranslationX(paddingLeft);
                    iv_come.setTranslationY(-iv_come.getY() + iv_come.getHeight());
                    iv_come.setVisibility(View.VISIBLE);
                }
            });

            //上升动画
            bouncer.play(rotation2)
                    .with(translationY)
                    .after(bounceInLeft);
//
//            ObjectAnimator pulseY = ObjectAnimator.ofFloat(iv_come, "scaleY", 1, 1.3f, 1);
//            ObjectAnimator pulseX = ObjectAnimator.ofFloat(iv_come, "scaleX", 1, 1.3f, 1);
//            pulseX.setRepeatCount(2);
//            pulseY.setRepeatCount(2);
//            pulseX.addListener(new AnimatorListenerAdapter() {
//                @Override
//                public void onAnimationEnd(Animator animation) {
//                    super.onAnimationEnd(animation);
//                    iv_come.setVisibility(View.INVISIBLE);
//                }
//            });

//            ObjectAnimator bounce = ObjectAnimator.ofFloat(target, "translationY"
//                    , top, top, top - 45,
//                    top, top - 25, top, top);
//            bounce.setRepeatCount(2);

            float x = (iv_come.getWidth() - iv_come.getPaddingLeft() - iv_come.getPaddingRight()) / 2
                    + iv_come.getPaddingLeft();
            float y = iv_come.getHeight() - iv_come.getPaddingBottom();
            ObjectAnimator stand1 = ObjectAnimator.ofFloat(iv_come, "pivotX", x, x, x, x, x);
            ObjectAnimator stand2 = ObjectAnimator.ofFloat(iv_come, "pivotY", y, y, y, y, y);
            ObjectAnimator stand3 = ObjectAnimator.ofFloat(iv_come, "rotationX", 55, -30, 15, -15, 0);
            stand1.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                }
            });
            
//            stand1.setRepeatCount(2);
//            stand2.setRepeatCount(2);
//            stand3.setRepeatCount(2);
            //提示动画
//            bouncer.play(pulseY)
////                    .with(bounce)
//                    .with(pulseX)
//                    .after(translationY);
            bouncer.play(stand1)
                    .with(stand2)
                    .with(stand3)
                    .after(translationY);

            AnimatorSet bouncer2 = new AnimatorSet();

            ObjectAnimator outRight = ObjectAnimator.ofFloat(target, "translationX", paddingLeft, paddingLeft + target.getWidth() / 5, -target.getWidth());
            outRight.setInterpolator(new AccelerateInterpolator());
            ObjectAnimator outRight2 = ObjectAnimator.ofFloat(iv_come, "translationX", paddingLeft, paddingLeft + target.getWidth() / 5, -target.getWidth());
            outRight2.setInterpolator(new AccelerateInterpolator());
            //消失
            bouncer2.play(outRight).with(outRight2)
                    .after(2000);

            getAnimatorAgent().playSequentially(bouncer, bouncer2);
            getAnimatorAgent().addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    isRunning = false;
                    iv_come.setVisibility(View.INVISIBLE);
                    iv_robot.setVisibility(View.INVISIBLE);
                }
            });

            iv_robot.setVisibility(View.VISIBLE);
            getAnimatorAgent().start();
        }
    }
}
